<?php
namespace app\admin\controller;
use framework\ext\Check;
/* *
 * 自定义单页功能
 * */
class NavController extends \app\base\controller\AdminController{
 
    //首页
    public function index(){
        $act = form('get.act');
        if($act == 'del'){
            $id = form('get.id',0,intval);
            $result =  model('nav')->_delete($id);
            if($result){
                $this->jsonMsg($result);
            }else{
                $this->jsonMsg('删除成功',1);
            }
        }else{
            $this->pid = form('get.id',0,intval);
            $page = form('get.page',0,intval);
            $this->list = model('nav')->pager($page,20)->_select(array('pid'=>$this->pid));
            dump( model('nav')->getsql);
            $this->pager = $this->getPage(model('nav')->pager,array(id=>$this->pid));
            $this->path = model('nav')->path($this->pid,'nav/index');
            $this->display();
        }
    }
    
    //编辑
    public function edit(){
        if($this->isPost()){
            $msg = Check::rule(
                array(check::must($_POST['name']),'菜单名称必须填写'),
                array(check::must($_POST['url']),'菜单连接必须填写'));
            if(true !== $msg){
                $this->pageMsg($msg);
            }
            $data = array();
            $data['name'] = form('post.name');
            $data['url'] =  form('post.url');
            $data['uptime'] = time();
            $fid = form('post.fid',0,intval);
            $pid =  form('post.pid',0,intval);
            if($fid){
                $condition['id'] = $fid;
                $rel = model('nav')->_update($data,$condition);
            }else{
                $data['pid'] = $pid;
                $data['neworder'] = 0;
                $rel = model('nav')->_update($data);
            }
            if($rel){
                $this->redirect(url('nav/index',array('id' => $pid)));
            }else{
                $this->pageMsg('菜单设置失败');
            }
        }else{
            $this->pid = form('get.pid',0,intval);
            $this->fid = form('get.id',0,intval);
            $pathid = $this->pid?$this->pid:$this->fid;
            $this->path = model('nav')->path($pathid,'nav/index');
            $counts = model('nav')->count_path($pathid);
            $n = $this->fid?4:3;
            if(count($counts) >= $n){
                $this->pageMsg('菜单设置禁止超过三级目录');
            }
            $this->info = model('nav')->_find(array('id' =>$this->fid));
            $this->display();
        }
    }

    //字段排序
    public function uporter(){
        $fid =  intval($_POST['id']);
        $condition['id'] = $fid;
        $data['neworder'] = intval($_POST['sorter']);
        $info = model('nav')->_update($data,$condition);
        if($info){
            $this->jsonMsg('自定义排序成功',1);
        }else{
            $this->jsonMsg('自定义排序失败');
        }
    }
}